/**
 * 播种面板
 */
class PlantPanel extends PanelBase {

	public constructor() {
		super();
		this.skinName = PlantSkin;
	}


	// 植物列表
	private listPlant: eui.List;

	protected createChildren() {
		super.createChildren();
		// 初始化list
		this.listPlant.dataProvider = null;
		this.listPlant.itemRenderer = PlantItem;
		// this.listPlant.addEventListener(egret.TouchEvent.TOUCH_TAP, this.listTap, this);
		CommonData.plantInfo((e) => {
			this.listPlant.dataProvider = new eui.ArrayCollection(e);
		})
		PlayerSelfData.landInfo((land_info) => {
			if (PlayerSelfData.tap_land_id == 0) {
				this.playLandInfoOn(land_info)
			} else {
				var key = 0;
				for (var k = 0; k < land_info.length; k++) {
					if (land_info[k]['id'] == PlayerSelfData.tap_land_id) {
						key = k;
						break;
					}
				}
				if (
					typeof land_info[key] != "undefined" &&
					typeof land_info[key]["plant_id"] != "undefined" &&
					land_info[key]["plant_id"] != 0
				) {
					// 选的土地已有植物
					this.playLandInfoOn(land_info)
				}
			}
		})
	}



	/**
	 * 处理选中土地
	 */
	private playLandInfoOn(land_info) {
		for (var i = 0; i < PlayerSelfData.landInfoLength; i++) {
			if (typeof land_info[i]["plant_id"] == "undefined" || land_info[i]["plant_id"] == 0) {
				PlayerSelfData.tap_land_id = land_info[i]["id"];
				break;
			}
		}
		if (PlayerSelfData.tap_land_id == 0 && (typeof land_info[0]["plant_id"] != "undefined" && land_info[0]["plant_id"] != 0)) {
			SceneManager.ins.alert("土地已经种满啦~~")
			return;
		}
	}


	/**
	 * 点击播种
	 */
	// private listTap(e) {
	// 	// 点击的是第几个
	// 	console.log(this.listPlant.$getSelectedIndex());
	// 	console.log(this.listPlant.dataProvider['source'][this.listPlant.$getSelectedIndex()])
	// }

}